package com.yoomiito.app.ui.my;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.gyf.barlibrary.ImmersionBar;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.yoomiito.app.Constant;
import com.yoomiito.app.R;
import com.yoomiito.app.adapter.my.MyIncome2Adapter;
import com.yoomiito.app.adapter.my.MyIncomeAdapter;
import com.yoomiito.app.base.App;
import com.yoomiito.app.base.BaseActivity;
import com.yoomiito.app.base.WebViewActivity;
import com.yoomiito.app.model.IncomeTypeData;
import com.yoomiito.app.model.bean.IncomeBean;
import com.yoomiito.app.model.bean.MultipleItem;
import com.yoomiito.app.model.bean.TabBean;
import com.yoomiito.app.model.my.IncomeInfo;
import com.yoomiito.app.model.my.MyHistoryIncomeInfo;
import com.yoomiito.app.present.my.activity.MyIncomerPresenter;
import com.yoomiito.app.present.my.activity.MyMonthOrderPresenter;
import com.yoomiito.app.ui.anyview.income.IncomeTypeView;
import com.yoomiito.app.ui.fragment.my.MyHistoryIncomeFragment;
import com.yoomiito.app.ui.fragment.my.MyOrderBaseFragment;
import com.yoomiito.app.ui.my.psw.PswManagerActivity;
import com.yoomiito.app.utils.AllCode;
import com.yoomiito.app.utils.ResUtil;
import com.yoomiito.app.utils.SpannableUtils;
import com.yoomiito.app.widget.dialog.AccountCancelDialog;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.droidlover.xdroidmvp.router.Router;

/**
 * 历史总收入
 */
public class MyIncomeActivity extends BaseActivity<MyIncomerPresenter> {
    @BindView(R.id.tv_money)
    TextView tvMoney;
    @BindView(R.id.tv1)
    TextView tv1;
    @BindView(R.id.tv11)
    TextView tv11;
    @BindView(R.id.tv2)
    TextView tv2;
    @BindView(R.id.tv22)
    TextView tv22;
    @BindView(R.id.tv3)
    TextView tv3;
    @BindView(R.id.tv33)
    TextView tv33;
    @BindView(R.id.tv4)
    TextView tv4;
    @BindView(R.id.tv44)
    TextView tv44;
    @BindView(R.id.tv5)
    TextView tv5;
    @BindView(R.id.tv55)
    TextView tv55;
    @BindView(R.id.tv6)
    TextView tv6;
    @BindView(R.id.tv66)
    TextView tv66;
    @BindView(R.id.tv7)
    TextView tv7;
    @BindView(R.id.tv77)
    TextView tv77;
    private int page = 1;
    private int type = 1;
    private BaseQuickAdapter mAdapter;
    private LinearLayout empty;
    private int agent;
    @BindView(R.id.rcy)
    RecyclerView recyclerView;
    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    private int roleId;


    @Override
    public int getLayoutId() {
        return R.layout.activity_my_income;
    }


    @Override
    protected void initImmersionBar() {
        ImmersionBar.with(this).titleBar(findViewById(R.id.layout_top)).init();
    }

    @Override
    public MyIncomerPresenter newP() {
        return new MyIncomerPresenter(App.mAppConfig);
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        agent = getIntent().getIntExtra("agent", 0);
        roleId = getIntent().getIntExtra("roleId",0);
        if(roleId == 0){
            findViewById(R.id.ll3).setVisibility(View.GONE);
            findViewById(R.id.ll4).setVisibility(View.GONE);
            findViewById(R.id.ll5).setVisibility(View.GONE);
            findViewById(R.id.ll6).setVisibility(View.GONE);
            findViewById(R.id.ll7).setVisibility(View.GONE);
        }else {
            if (agent == 1) {
                findViewById(R.id.ll7).setVisibility(View.VISIBLE);
            } else {
                findViewById(R.id.ll7).setVisibility(View.GONE);
            }
        }

        findViewById(R.id.back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);

        empty = findViewById(R.id.ll_no_data);
        getP().getAccountList();
        getP().getHistoryList(page, type);
        smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                getP().getHistoryList(page, type);
            }
        });
        smartRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                getP().getHistoryList(page, type);
            }
        });
        setAdapter();
    }

    @OnClick({R.id.ll1, R.id.ll2, R.id.ll3, R.id.ll4, R.id.ll5, R.id.ll6, R.id.ll7})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll1:
                page = 1;
                type = 1;
                getP().getHistoryList(page, type);

                tv1.setTextColor(ContextCompat.getColor(this, R.color.color_blue));
                tv11.setTextColor(ContextCompat.getColor(this, R.color.color_blue));
                tv2.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                tv22.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                tv3.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                tv33.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                tv4.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                tv44.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                tv5.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                tv55.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                tv6.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                tv66.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                tv7.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                tv77.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                break;
            case R.id.ll2:
                page = 1;
                type = 2;
                getP().getHistoryList(page, type);
                tv1.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                tv11.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                tv2.setTextColor(ContextCompat.getColor(this, R.color.color_blue));
                tv22.setTextColor(ContextCompat.getColor(this, R.color.color_blue));
                tv3.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                tv33.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                tv4.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                tv44.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                tv5.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                tv55.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                tv6.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                tv66.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                tv7.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                tv77.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                break;
            case R.id.ll3:
                page = 1;
                type = 3;
                getP().getHistoryList(page, type);
                tv1.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                tv11.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                tv2.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                tv22.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                tv3.setTextColor(ContextCompat.getColor(this, R.color.color_blue));
                tv33.setTextColor(ContextCompat.getColor(this, R.color.color_blue));
                tv4.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                tv44.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                tv5.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                tv55.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                tv6.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                tv66.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                tv7.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                tv77.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                break;
            case R.id.ll4:
                page = 1;
                type = 5;
                getP().getHistoryList(page, type);
                tv1.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                tv11.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                tv2.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                tv22.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                tv3.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                tv33.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                tv4.setTextColor(ContextCompat.getColor(this, R.color.color_blue));
                tv44.setTextColor(ContextCompat.getColor(this, R.color.color_blue));
                tv5.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                tv55.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                tv6.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                tv66.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                tv7.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                tv77.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                break;
            case R.id.ll5:
                page = 1;
                type = 6;
                getP().getHistoryList(page, type);
                tv1.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                tv11.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                tv2.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                tv22.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                tv3.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                tv33.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                tv4.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                tv44.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                tv5.setTextColor(ContextCompat.getColor(this, R.color.color_blue));
                tv55.setTextColor(ContextCompat.getColor(this, R.color.color_blue));
                tv6.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                tv66.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                tv7.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                tv77.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                break;
            case R.id.ll6:
                page = 1;
                type = 4;
                getP().getHistoryList(page, type);
                tv1.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                tv11.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                tv2.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                tv22.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                tv3.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                tv33.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                tv4.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                tv44.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                tv5.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                tv55.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                tv6.setTextColor(ContextCompat.getColor(this, R.color.color_blue));
                tv66.setTextColor(ContextCompat.getColor(this, R.color.color_blue));
                tv7.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                tv77.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                break;
            case R.id.ll7:
                page = 1;
                type = 7;
                getP().getHistoryList(page, type);
                tv1.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                tv11.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                tv2.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                tv22.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                tv3.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                tv33.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                tv4.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                tv44.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                tv5.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                tv55.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                tv6.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                tv66.setTextColor(ContextCompat.getColor(this, R.color.color_white));
                tv7.setTextColor(ContextCompat.getColor(this, R.color.color_blue));
                tv77.setTextColor(ContextCompat.getColor(this, R.color.color_blue));
                break;

        }
        setAdapter();
    }


    private void setAdapter() {

        if (type == 1 || type == 2 || type == 5) {
            mAdapter = new MyIncomeAdapter();
        } else {
            mAdapter = new MyIncome2Adapter(type);
        }
        recyclerView.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (type == 6 || type == 7) {
                    IncomeInfo info = (IncomeInfo) mAdapter.getData().get(position);
                    Intent intent = new Intent(MyIncomeActivity.this, MyIncomeDetailActivity.class);
                    intent.putExtra("month",info.month);
                    intent.putExtra("type",type);
                    startActivity(intent);

                }

            }
        });
    }

    public void setData(IncomeBean data) {
        tvMoney.setText(data.totalMoney);
        tv1.setText(data.goodsMoney);
        tv2.setText(data.vipMoney);
        tv3.setText(data.teamMoney);
        tv4.setText(data.offlineMoney);
        tv5.setText(data.creditCard);
        tv6.setText(data.bonusMoney);
        tv7.setText(data.agentMoney);
    }

    public void setData1(List<IncomeInfo> data) {
        if (page == 1) {
            if (data == null || data.size() <= 0) {
                empty.setVisibility(View.VISIBLE);
            }else {
                empty.setVisibility(View.GONE);
            }
            mAdapter.setNewData(data);
            smartRefreshLayout.setNoMoreData(false);
        } else {
            empty.setVisibility(View.GONE);
            smartRefreshLayout.finishLoadMore(true);
            if (data == null || data.size() <= 0) {
                Toast.makeText(context, "没有更多了", Toast.LENGTH_SHORT).show();
                smartRefreshLayout.finishLoadMoreWithNoMoreData();
            } else {
                mAdapter.addData(data);
            }
        }
        smartRefreshLayout.finishRefresh();
        smartRefreshLayout.finishLoadMore();
    }

}
